package com.niit.agricstore.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.agricstore.entity.Aftersale;
import com.niit.agricstore.entity.Logistics;
import com.niit.agricstore.entity.Order;
import com.niit.agricstore.enu.AfterSaleStatus;
import com.niit.agricstore.enu.AfterSaleType;
import com.niit.agricstore.enu.OrderStatus;
import com.niit.agricstore.mapper.OrderMapper;
import com.niit.agricstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService{

    @Autowired
    OrderMapper orderMapper;
    /**订单信息**/
    //查询已发货的订单信息
    @Override
    public List<Order> getShippedOrders() {
        return orderMapper.getShippedOrders();
    }
    //根据订单编号查询物流信息
    @Override
    public Logistics getLogisticsInfo(int orderId) {
        return orderMapper.getLogisticsInfo(orderId);
    }
    //更新订单的收货状态
    @Override
    public void updateOrderShippingStatus(int orderId, String shippingStatus) {
        orderMapper.updateOrderShippingStatus(orderId, shippingStatus);
    }
    /**订单管理**/
    //确认订单、更新发货状态
    @Override
    public void confirmOrder(int orderId) {
        orderMapper.confirmOrder(orderId);
        orderMapper.updateOrderStatus(orderId, OrderStatus.SHIPPED.getStatus());
    }
    //获取退款申请列表
    @Override
    public List<Aftersale> getAftersaleList() {
        return orderMapper.getAftersaleList();
    }
    //同意退款申请
    @Override
    public void approveRefundApplication(int aftersale_id) {
        orderMapper.updateRefundStatus(aftersale_id, AfterSaleStatus.APPROVED.getStatus());
    }
    //拒绝退款申请
    @Override
    public void rejectRefundApplication(int aftersale_id) {
        orderMapper.updateRefundStatus(aftersale_id, AfterSaleStatus.REJECTED.getStatus());
    }
    //更新订单状态
    @Override
    public void updateOrderStatus(int orderId, int status) {
        orderMapper.updateOrderStatus(orderId, status);
    }
    //添加售后记录
    @Override
    public void addAfterSale(Aftersale aftersale) {
        orderMapper.addAfterSale(aftersale);
    }
    //确认售后申请
    @Override
    public void confirmAfterSale(int aftersale_id, String aftersale_address) {
        orderMapper.updateAfterSaleStatus(aftersale_id, AfterSaleStatus.COMPLETED.getStatus());
        Aftersale aftersale = orderMapper.getAfterSaleById(aftersale_id);
        if (AfterSaleType.valueOf(aftersale.getAftersale_type()) == AfterSaleType.RETURN) {
            orderMapper.updateOrderStatus((int) aftersale.getAftersale_order_id(), OrderStatus.REFUNDING.getStatus());
        } else if (AfterSaleType.valueOf(aftersale.getAftersale_type()) == AfterSaleType.REFUND) {
            orderMapper.updateOrderStatus((int) aftersale.getAftersale_order_id(), OrderStatus.REFUNDED.getStatus());
        }
    }

    //拒绝售后申请
    @Override
    public void rejectAfterSale(int aftersale_id, String aftersale_reason) {
        orderMapper.updateAfterSaleStatus(aftersale_id, AfterSaleStatus.REJECTED.getStatus());
    }
    //获取售后记录详情
    @Override
    public Aftersale getAfterSaleById(int aftersale_id) {
        return orderMapper.getAfterSaleById(aftersale_id);
    }


}
